package com.huige.service.impl;

import com.huige.aop.MyLog;
import com.huige.mapper.DeptMapper;
import com.huige.mapper.EmpMapper;
import com.huige.pojo.Dept;
import com.huige.pojo.DeptLog;
import com.huige.service.DeptLogService;
import com.huige.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: DeptServiceImpl
 * Package: com.huige.service.impl
 * Description:
 *
 * @Author 朱志辉
 * @Create 2024/5/3 14:59
 * @Version 1.0
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;


//    @MyLog
    @Override
    public List<Dept> list() {
        List<Dept> list = deptMapper.list();
        return list;
    }

    @MyLog
    @Transactional(rollbackFor = Exception.class)//开启事务
    @Override
    public void delete(Integer id) throws Exception {
        try {
            deptMapper.deleteById(id);//根据id删除部门数据
//            int i = 1 / 0;//抛出异常
//        if (true) {
//            throw new Exception("出错了...");
//        }
        } finally {
            empMapper.deleteByDeptId(id);//根据部门的id来删除部门下的员工信息}
            DeptLog deptLog = new DeptLog();
            deptLog.setCreateTime(LocalDateTime.now());
            deptLog.setDescription("执行了解散部门操作，此次解散是" + id + "号部门");
            deptLogService.insert(deptLog);

        }

    }

    @Override
    public void insert(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertByname(dept);
    }

    @Override
    public Dept getByID(Integer id) {
//        int i = 1 / 0;
        Dept dept = deptMapper.getByID(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }


}





